package com.jeesite.modules.dataScreen.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeesite.modules.dataScreen.dao.AirportDao;
import com.jeesite.modules.dataScreen.web.vo.avition.AirportInformationKVVo;
import com.jeesite.modules.dataScreen.web.vo.avition.AirportInformationYwVo;
import com.jeesite.modules.dataScreen.web.vo.railway.TrainDelayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AirportService {
    @Autowired
    private AirportDao airportDao;


    public JSONObject passageDetention(String status) {

        JSONObject jsonObject = new JSONObject();
        List<JSONObject> airportInformation = airportDao.passageDetention(status);
        for (JSONObject object : airportInformation) {
            String lateTime = object.getString("latetime");
            String time = lateTime.substring(0, lateTime.indexOf("日")+1);
            object.remove("latetime");
            object.put("latetime", time);

        }

        int sum = airportInformation.stream()
                .mapToInt(a -> {
                    try {
                        return Integer.parseInt(a.getString("latenumber"));
                    }catch (Exception e){
                        return 0;
                    }
                })
                .sum();
        jsonObject.put("total",sum);
        jsonObject.put("data",airportInformation);
        return jsonObject;
    }

    public JSONObject dmjhbywhbjPage(String status) {
        JSONObject result = new JSONObject();
        IPage<AirportInformationYwVo> page = new Page<>();
        page.setCurrent(1).setSize(Integer.MAX_VALUE);
        IPage<AirportInformationYwVo> airportInformationYwVoIPage = airportDao.dmjhbywhbjPage(page,status);

        List<AirportInformationYwVo> records = airportInformationYwVoIPage.getRecords();
        for (AirportInformationYwVo record : records) {
            String time = record.getLateTime().substring(0, record.getLateTime().indexOf("日") + 1);
            record.setLateTime(time);

        }
        result.put("datas", records);
        result.put("total", records.stream().filter(e -> !StrUtil.equals(e.getStatus(), "结束")).mapToInt(e -> {
            try {
                return Integer.parseInt(e.getLateNumber());
            } catch (Exception exception) {
                return 0;
            }
        }).sum());
        return result;
    }

    public List<AirportInformationYwVo> detail(String eventName) {

        List<AirportInformationYwVo> airportInformationYwVos =airportDao.detail(eventName);
        List<AirportInformationYwVo> result = airportInformationYwVos.stream().filter(airportInformationYwVo -> {
            if ("发生".equals(airportInformationYwVo.getStatus())) {
                airportInformationYwVo.setProcess(airportInformationYwVo.getEventContent());
                return true;
            }
            if ("处置中".equals(airportInformationYwVo.getStatus())) {
                airportInformationYwVo.setProcess(airportInformationYwVo.getDisposalMeasure());
                return true;
            }
            if ("结束".equals(airportInformationYwVo.getStatus())) {
                airportInformationYwVo.setProcess(airportInformationYwVo.getDisposalMeasure());
                return true;
            }
            return true;
        }).collect(Collectors.toList());
        return result;
    }

    public List<JSONObject> getRetentionProcess(String eventName) {

        List<JSONObject> jsonObjectList =airportDao.getRetentionProcess(eventName);
        List<JSONObject> result = jsonObjectList.stream().filter(trainDelayVO -> {
            if ("发生".equals(trainDelayVO.get("status"))) {
                trainDelayVO.put("process",trainDelayVO.getString("eventcontent"));
                return true;
            }
            if ("处置中".equals(trainDelayVO.get("status"))) {
                trainDelayVO.put("process",trainDelayVO.getString("disposalmeasure"));
                return true;
            }

            if ("结束".equals(trainDelayVO.get("status"))) {
                trainDelayVO.put("process",trainDelayVO.getString("disposalmeasure"));
                return true;
            }
            return true;
        }).collect(Collectors.toList());
        return result;
    }
}
